<%= turbo_frame_tag 'time_entry_form' do %>

<%= start_pending_app_tour_tag("time_entries/form") %>

<%= render Modal::DialogComponent.new title: "#{TimeEntry.model_name.human} - #{l(time_entry.reference_date)}", icon: icon_for(:time_entries) do %>
  <div class="">
    <%= form_with(model: time_entry, html: {
        class: 'flex flex-col gap-2',
        data: {
          controller: 'dependent-fields form time-entry--form',
          action: "modal:closed@window->time-entry--form#continueTour"
        }
      }) do |f| %>
      <% if time_entry.errors.any? %>
        <div class="flex rounded-md w-full border-l-6 border-alert bg-alert bg-opacity-[15%] px-4 py-1 mb-5 shadow-sm md:p-4 text-sm text-alert">
          <%= time_entry.errors.full_messages.to_sentence.capitalize %>
        </div>
      <% end %>

      <%= f.hidden_field :reference_date %>
      <fieldset class="fieldset grow cpy-project-list tour--project-selection">
        <%= f.label :project_id, TimeEntry.human_attribute_name(:project), class: "fieldset-label" %>
        <%
          projects_for_select = Project.active.order(name: :asc).to_a

          if time_entry.persisted? and time_entry.project.archived?
            projects_for_select.push(time_entry.project)
          end
        %>
        <%= f.select :project_id, options_from_collection_for_select(projects_for_select, :id, :name, time_entry.project_id), { include_blank: t('select_one') }, {required: true, style: "width: 100%",
         autofocus: (time_entry.new_record? && time_entry.project_id.blank?),
        data: {
          controller: 'select2',
          dependent_fields_target: "hasDependents",
          dependant_turbo_frame_selector: '#project_dependent_fields'
        } } %>
      </fieldset>

      <%= render partial: 'form_project_dependent_fields', locals: { form: f } %>

      <fieldset class="fieldset tour--description-field">
        <%= f.label :description, TimeEntry.human_attribute_name(:description), autofocus: time_entry.persisted?, class: "fieldset-label" %>

        <div data-controller='resizable-input' class="relative" >
          <div data-resizable-input-target="replica"
          class="break-all textarea text-sm p-2 whitespace-pre-line"></div>

          <%= f.text_area :description,
            class: "text-sm p-2 textarea absolute w-full left-0 top-0 right-0 bottom-0 overflow-y-hidden",
            "data-resizable-input-target": "input",
            "data-action": "keydown.ctrl+enter->form#submit keydown.meta+enter->form#submit"
            %>
        </div>
        <p class="label mt-1"><%= t('.save_on_command_enter_html') %></span>
      </fieldset>

      <div class="fieldset tour--logged-time" data-controller="time-entry--time-input-formatter" data-time-entry--time-input-formatter-format-value="<%= current_user.preferences.time_entry_time_format %>">
        <div class="flex justify-between items-center">
          <%= f.label :total_logged_time_in_minutes, "*" + TimeEntry.human_attribute_name(:total_logged_time), class: "label" %>

          <div class="flex gap-2 items-center">
            <a href="<%= update_preferences_profile_path(time_entry_time_format: 'minutes') %>" data-turbo-method="patch"
                    data-time-entry--time-input-formatter-target="tab"
                    data-format="minutes"
                    data-action="click->time-entry--time-input-formatter#selectFormat"
                    class="tab-item tab-item-xs <%= current_user.preferences.time_entry_time_format == 'minutes' ? 'tab-item--active' : '' %>">
              <%= t('minutes') %>
            </a>
            <a href="<%= update_preferences_profile_path(time_entry_time_format: 'hours') %>" data-turbo-method="patch"
                    data-time-entry--time-input-formatter-target="tab"
                    data-format="hours"
                    data-action="click->time-entry--time-input-formatter#selectFormat"
                    class="tab-item tab-item-xs <%= current_user.preferences.time_entry_time_format == 'hours' ? 'tab-item--active' : '' %>">
              <%= t('hours') %>
            </a>
          </div>
        </div>

        <%= f.hidden_field :total_logged_time_in_minutes,
            data: {
              'time-entry--time-input-formatter-target': "input"
            } %>

        <input type="text"
          id="time_entry_total_logged_time"
          class="input w-full input-xl cpy-input-placeholder"
          value="<%= time_entry.total_logged_time_in_minutes %>" data-time-entry--time-input-formatter-target="placeholder"/>

      </div>

      <div class="flex gap-2 justify-end mt-2 tour--form-actions">
        <a class="cursor-pointer btn btn-ghost" data-action="click->modal#close">
          <%= t('actions.cancel') %>
        </a>

        <% text = time_entry.persisted? ? t('actions.update') : t('actions.create') %>
        <%= f.button text, class: "btn btn-primary" %>
      </div>
    <% end %>
  </div>
<% end %>
<% end %>
